<?php

class Application_Model_User extends Child_Model
{
    public function __construct($id = NULL, $fieldName = null, $field=null) {
        parent::__construct(new Application_Model_DbTable_Users, $id, $fieldName, $field);
    }

    public function getAllUsers(){
    
        return $this->_dbTable->fetchAll();
    }
    
    public function populateForm(){
        return $this->_row->toArray();
    }
    public function sendActivationEmail()
    {
        $mail = new Quani_Mail();
        $mail->addTo($this->_row->email);
        $mail->setSubject('Активация Аккаунта');
        $mail->setBodyView('activation',array('user' => $this));
        $mail->send();
    }
    public function sendSuccessEmail()
    {
        $mail = new Quani_Mail();
        $mail->addTo($this->_row->email);
        $mail->setSubject('Поздравляем');
        $mail->setBodyView('success',array('user' => $this));
        $mail->send();
    }
//    public function sendRemindeEmail($newpass)
//    {
//        $mail = new Quani_Mail();
//        $mail->addTo($this->_row->email);
//        $mail->setSubject('Востанова');
//        $mail->setBodyView('reminde',array(
//            'user' => $this,
//            'password'=>$newpass,
//            ));
//        $mail->send();
//    }
    public function authorize($username,$password){
        $auth = Zend_Auth::getInstance();
        
        $authAdapter = new Zend_Auth_Adapter_DbTable(
                Zend_Db_Table::getDefaultAdapter(),
                'users',
                'username',
                'password',
                'sha(?) and activated = 1'
                );
        $authAdapter->setIdentity($username)
                ->setCredential($password);
        
        $result = $auth->authenticate($authAdapter);
        if($result->isValid()){
            $storage = $auth->getStorage();
            $storage->write($authAdapter->getResultRowObject(null,array('password')));
            return true;
        }
        return false;
    }
    
}

